package org.ellisande.grid.tags;

import java.util.EnumSet;

import javax.servlet.jsp.JspWriter;

import org.ellisande.grid.tags.support.TagBuilder;
import org.ellisande.tld.annotations.Attribute;
import org.ellisande.tld.annotations.Description;
import org.ellisande.tld.annotations.Info;
import org.ellisande.tld.annotations.Tag;

@Tag
@Info("Tag for rendering different types of buttons to the screen. Type of button, size, disabled, and title are all customizable.")
public class ButtonTag extends AbstractTag {

	protected static final String BASE_STYLE = "btn";

	private static final long serialVersionUID = -2594969333478375829L;

	protected static enum Type {
		DEFAULT(""), PRIMARY("btn-primary"), INFO("btn-info"), SUCCESS(
				"btn-success"), WARNING("btn-warning"), DANGER("btn-danger"), INVERSE(
				"btn-inverse");

		private String type;

		private Type(String type) {
			this.type = type;
		}

		public String toString() {
			return type;
		}
	}

	protected static enum Size {
		DEFAULT(""), LARGE("btn-large"), SMALL("btn-small"), MINI("btn-mini");

		private String size;

		private Size(String size) {
			this.size = size;
		}

		public String toString() {
			return size;
		}
	}

	private static enum Action {
		DEFAULT("button"), RESET("reset"), SUBMIT("submit");

		private String type;

		private Action(String type) {
			this.type = type;
		}

		public String getType() {
			return type;
		}

		public boolean equals(String input) {
			return name().equalsIgnoreCase(input);
		}
		
		public static Action getAction(String actionName){
			for(Action action : values()){
				if(action.equals(actionName)){
					return action;
				}
			}
			
			return DEFAULT;
		}
		
		
	}

	@Attribute
	@Description("Defines the type of button to be rendered. Allowed values: default | primary | info | success | warning | danger | inverse")
	protected Type type = Type.DEFAULT;

	@Attribute
	@Description("Defines the size of the button to be rendered. Allowed values: default | large | small | mini")
	protected Size size = Size.DEFAULT;

	@Attribute
	@Description("Defines whether or not the displayed button should disabled. Allowed values: true | false. Default: false")
	protected String disabled = "";

	@Attribute
	@Description("Defines the text of the button to be rendered.")
	protected String title = "";
	
	@Attribute
	@Description("Defines the action type of this button. Used for buttons that interact with forms. If the default type is used, button actions must be defined in an onclick attribute. Allowed values: default | reset | submit. Default : default.")
	private Action action = Action.DEFAULT;

	public void setType(String type) {
		for (Type currentType : EnumSet.allOf(Type.class)) {
			if (currentType.name().equalsIgnoreCase(type)) {
				this.type = currentType;
				return;
			}
		}

		this.type = Type.DEFAULT;
	}

	public void setSize(String size) {
		for (Size currentSize : EnumSet.allOf(Size.class)) {
			if (currentSize.name().equalsIgnoreCase(size)) {
				this.size = currentSize;
				return;
			}
		}

		this.size = Size.DEFAULT;
	}

	public void setDisabled(String disabled) {
		if ("true".equalsIgnoreCase(disabled)) {
			this.disabled = "disabled";
			return;
		}

		this.disabled = "";

	}

	@Override
	public void release() {
		super.release();
		size = null;
		type = null;
		disabled = null;
		title = null;
	}

	public void setTitle(String title) {
		if (title == null) {
			this.title = "";
			return;
		}

		this.title = title;
	}

	@Override
	public void startTag() throws Exception {
		TagBuilder button = createTagBuilder("button");
		button.addStyle(BASE_STYLE);
		button.addStyle(type.toString());
		button.addStyle(size.toString());
		button.addAttribute("type", action.getType());
		if (!disabled.isEmpty()) {
			button.addStyle(disabled);
		}

		JspWriter out = pageContext.getOut();
		out.print(button.compileStartTag());
		out.print(title);
	}

	@Override
	public void endTag() throws Exception {
		TagBuilder button = createTagBuilder("button");
		JspWriter out = pageContext.getOut();
		out.print(button.compileEndTag());
	}

	public void setAction(String action) {
		this.action = Action.getAction(action);
	}
}
